//commandRun.cpp

#include <iostream>
#include <string>
#include "commandRun.h"
#include "task.h"

CommandRun::CommandRun(){

}

void CommandRun::ProgramLaunch() {
	std::cout << CMD_USERPROMPT;
	std::string command;
	std::cin >> command;
	while(command != CMD_EXIT) {
		executeCommand(command);
		std::cout << CMD_USERPROMPT;
		std::cin >> command;
	}
}

CommandRun::COMMAND_TYPE CommandRun::determineCommand(std::string command) {
	if( command == CMD_ADD) {
		return COMMAND_TYPE::ADD;
	} else if ((command==CMD_DELETE)) {
		return COMMAND_TYPE::DELETE;
	} else if ((command== CMD_DISPLAY)) {
		return COMMAND_TYPE::DISPLAY;
	} else if ((command== CMD_EXIT)) {
		return COMMAND_TYPE::EXIT;
	} else if ((command== CMD_SEARCH)) {
		return COMMAND_TYPE::SEARCH;
	} else if ((command== CMD_EDIT)) {
		return COMMAND_TYPE::EDIT;
	} else if ((command== CMD_COMPLETE)) {
		return COMMAND_TYPE::COMPLETE;
	} else {
		return COMMAND_TYPE::INVALID;
	}
}

void CommandRun::executeCommand(std::string command) {

	int index, dd, mm, yy, startTime, endTime;
	std::string taskInfo;

	switch(determineCommand(command)) {
	case ADD:
	{
		std::cin >> taskInfo >> startTime >> endTime >> dd >> mm >> yy;
		_cmdOperation.addTask(taskInfo, startTime, endTime, dd, mm, yy);
		break;
	}
	case DELETE:
	{
		int index;
		std::cin>> index;
		_cmdOperation.deleteTask(index);
		break;
	}
	case EDIT:
	{
		std::getline (std::cin, taskInfo);
		std::getline (std::cin, taskInfo);
		std::cin>> index >> startTime >> endTime >> dd >> mm >> yy;
		_cmdOperation.editTask(index, false, taskInfo, startTime, endTime, dd, mm, yy);
		break;
	}
	case EXIT:
	{
		exit(0);
		break;
	}
	case SEARCH:
	{
		std::string searchTerm;
		std::cin >> searchTerm;
		_cmdOperation.searchTasks(searchTerm);
		break;
	}
	case DISPLAY:
	{
		std::string displayType;
		std::cin >> displayType;
		if(displayType == "complete") {
		_cmdOperation.getCompleteTasks();
		} else if(displayType == "incomplete") {
			_cmdOperation.getIncompleteTasks();
		} else { 
			break;
		}
		break;
	}
	case INVALID:
	{
		break;
	}
}
}

const std::string CommandRun::CMD_ADD = "add";
const std::string CommandRun::CMD_EDIT = "edit";
const std::string CommandRun::CMD_DELETE = "delete";
const std::string CommandRun::CMD_DISPLAY = "display";
const std::string CommandRun::CMD_EXIT = "exit";
const std::string CommandRun::CMD_SEARCH = "search";
const std::string CommandRun::CMD_COMPLETE = "complete";
//const std::string CommandRun::CMD_SHOWCOMPLETE = "show completed";
//const std::string CommandRun::CMD_SHOWNCOMPLETE = "show uncompleted";
co nst std::string CommandRun::CMD_USERPROMPT= "command: ";